package com.saturn.titan.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.saturn.titan.core.bean.BaseArea;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.repository.mybatis.BaseAreaDao;

/**
 * 单据编码生成服务类
 * @author Administrator
 *
 */
@Component
@Transactional(readOnly = true)
public class AreaService {

	@Autowired
	private BaseAreaDao baseAreaDao;
	
	public List<BaseArea> findAllArea(){
		return baseAreaDao.select(new BaseArea());
	}

	@Transactional(readOnly = false)
	public void deleteById(Long id) {
		baseAreaDao.deleteById(id);
		CRMCache.AREA_CACHE.remove(id);
	}

	@Transactional(readOnly = false)
	public void add(BaseArea a) {
		baseAreaDao.insert(a);
		CRMCache.AREA_CACHE.put(a.getId(),a);
	}

	@Transactional(readOnly = false)
	public void deleteNode(Long id) {
		BaseArea b = new BaseArea();
		b.setParentId(id);
		baseAreaDao.deleteById(id);
		baseAreaDao.delete(b);
		
		CRMCache.AREA_CACHE.remove(id);
	}

	@Transactional(readOnly = false)
	public void update(BaseArea a) {
		baseAreaDao.update(a);
		CRMCache.AREA_CACHE.put(a.getId(),baseAreaDao.selectById(a.getId()));
	}

	public List<BaseArea> findArea(BaseArea ba) {
		return baseAreaDao.select(ba);
	}
	
}
